require(['util', 'validator'], function () {
    var $abnEmotionScoreForm = $("#abnEmotionScoreForm");

    var corpId = "",
        seleObj = {};

    $("#show_data").hide();
    $("#abnEmotionScoreBnt").hide();

    //左侧树点击事件
    commonFun.leftTreeNodeClickObj['funCheckEmotionaScoreSet'] = function (event, treeId, treeNode) {
        if (treeNode.type === 'CAR' || treeNode.type === 'DRIVER') {
            $.messager.alert("提示", "请选择组织机构或线路，进行参数设置。", "info");
            return;
        }
        seleObj = treeNode;
        corpId = treeNode.corpId;
        $("#corpId").val(corpId);
        showInterface();
    };

    //根据后台返回的数据，动态生成界面
    var showInterface = function () {
        $.ajaxExt({
            url: "/abnEmotionScore/searchAbnEmotionScoreInfo.htm",
            data: {"corpId": corpId, "organId": seleObj.id}
        }).done(function (data) {
            var currData = data;
            //清空界面，后面再根据数据渲染界面
            $("#u_tip").remove();
            $("#sele_msg").empty();
            $('#aeScoreWV').empty();
            $('#aeDedPoint').empty();
            $('#aeScoreAC').empty();
            //显示界面
            $("#show_data").show();
            $("#abnEmotionScoreBnt").show();
            //根据数据渲染界面
            var seleObjId = seleObj.id,
                currObjId = currData[0].organLineId;
            $("#sele_msg").append("<h1 class='line-title'>【" + seleObj.name + "】的情绪异常评分标准配置信息</h1>");
            $("#sele_msg").append("<input type='hidden' name='seleId' value='" + seleObjId + "'/>"
            + "<input type='hidden' name='corpId' value='" + corpId + "'/>"
            + "<input type='hidden' name='currId' value='" + currObjId + "'/>");
            $.each(currData, function (n, entity) {
                $("#aeScoreWV").append("<input type='hidden' name='chkIds' value='" + entity.chkId + "'/>");
                $('#aeScoreWV').append("<li>"
                + "<label class='label-text'>" + entity.chkName + "评分权重：</label>"
                + "<input type='text' name='aeScoreWVs' class='input-text'  data-validator='{required:true,number:true}' value='" + entity.weightValue + "'/> %"
                + "</li>");
                $('#aeDedPoint').append("<li>"
                + "<label class='label-text'>发生" + entity.chkName + "扣分：</label>"
                + "<input type='text' name='aeDedPoints' class='input-text'  data-validator='{required:true,number:true}' value='" + entity.deductValue + "'/>"
                + "</li>");
                $('#aeScoreAC').append("<li>"
                + "<label class='label-text'>允许" + entity.chkName + "次数：</label>"
                + "<input type='text' name='aeScoreACs' class='input-text'  data-validator='{required:true,number:true}' value='" + entity.allowCount + "'/>"
                + "</li>");
            });
        });
    };

    $("#abnEmotionScore_update").on("click", function () {
        if($.validator($('#abnEmotionScoreForm'))){
            $.messager.confirm("是否修改", "确定修改该机构的情绪异常评分标准配置信息？", function (r) {
                if (r) {
                    var formData = $abnEmotionScoreForm.serialize();
                    $.ajaxExt({
                        url: "/abnEmotionScore/uptAbnEmotionScoreInfo.htm",
                        data: formData
                    }).done(function (data) {
                    });
                }
            });
        }
    });

    $("#abnEmotionScore_reset").on("click", function () {
        showInterface();
    });
    
    //页面打开自动查询
    var nodes = commonFun.getLeftTreeSelectedNodes();
	if(nodes.length<1||nodes[0].type==="LINE"){
		seleObj = nodes[0];
        corpId = nodes[0].corpId;
        showInterface();
	}
});